package com.yiwei.ydd.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.adapter.RechargeRecordAdapter;
import com.yiwei.ydd.adapter.TabTopAdapter;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.RechargeRecordBean;
import com.yiwei.ydd.api.bean.RechargeTermDetailBean;
import com.yiwei.ydd.api.model.RechargeRecordModel;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.util.Dialog;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.OnLoadeMoreListener;
import com.yiwei.ydd.view.V19FrameLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class RechargeRecordActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.tablayout)
    RecyclerView tablayout;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;

    private Unbinder unbiner;
    private RechargeRecordAdapter adapter;
    private TabTopAdapter TabAdapter;
    private String QUICK_TYPE = "1";
    private String FENQI_TYPE = "2";
    private String type = QUICK_TYPE;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getRechargeRecord(page);
        }
    };

    //充值类型（1：快速充值，2：分期充值）
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge_record);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("充值记录");
        txtRight.setText("客服");
        txtRight.setVisibility(View.VISIBLE);
        TabAdapter = new TabTopAdapter(this);
        TabAdapter.setOnChooseListener(new TabTopAdapter.OnChooseListener() {
            @Override
            public void onClick(int poi) {
                if (poi == 0) {
                    type = QUICK_TYPE;
                } else {
                    type = FENQI_TYPE;
                }
                onLoadeMoreListener.setPage();
                getRechargeRecord(1);
            }
        });
        tablayout.setLayoutManager(new GridLayoutManager(this, 2));
        tablayout.setAdapter(TabAdapter);
        List<String> tab_titles = new ArrayList<>();
        tab_titles.add("即时充值");
        tab_titles.add("套餐充值");
        TabAdapter.update(tab_titles);


        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage();
                getRechargeRecord(1);
            }
        });
        adapter = new RechargeRecordAdapter(this);
        adapter.setOnChooseListener(new RechargeRecordAdapter.OnChooseListener() {
            @Override
            public void onClick(RechargeRecordModel.DatasBean item) {
                getRechargeTermDetail(item.coupon_money, item.git, item.money, item.periods);
            }
        });
        list.setAdapter(adapter);

        getRechargeRecord(1);
//        List<String> data = new ArrayList<>();
//        data.add("dadsa");
//        data.add("dadsa");
//        data.add("dadsa");
//        adapter.update(data);
    }

    private void getRechargeRecord(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        RechargeRecordBean bean = new RechargeRecordBean();
        bean.page = page;
        bean.type = type;
        Api.api_service.getRechargeRecord(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    adapter.setType(type);
                    if(adapter.getItemCount()==0){
                        layoutEmpty.setVisibility(View.VISIBLE);
                    }else {
                        layoutEmpty.setVisibility(View.GONE);
                    }

                });
    }

    //    ===========================获取分期详情===========================
    public void getRechargeTermDetail(String coupon_money, String git, String money, String periods) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }


        loadingDialog.show();
        RechargeTermDetailBean bean = new RechargeTermDetailBean();
        bean.coupon_money = coupon_money;
        bean.git = git;
        bean.money = money;
        bean.periods = periods;
        Api.api_service.getRechargeTermDetail(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    Dialog.showRechargeDetailDialog(this, result.datas, new Dialog.DialogDefaultClickListener() {
                        @Override
                        public void confirm() {

                        }

                        @Override
                        public void cancel() {

                        }
                    });
                });
    }
    //    ===========================获取分期详情===========================

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_submit, R.id.txt_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                Util.startActivity(this, ChatCenterActivity.class);
                break;
            case R.id.btn_submit:
                break;
        }
    }
}
